#include <iostream>
#include <vector>
#include <string>

using namespace std;

string findSymmetry(string n){
    for(int i = 0; i < n.size()/2; i++){
        n[n.size()-i-1] = n[i];
    }
    return n;
}

string findBigger(string n){
    int d = 1;
    for(int i = n.size()/2-1; d == 1; i--){
        if(i == 0 && n[i] == '9'){
            n[i] = '1',n[n.size()/2] = '0', d = 0; n.push_back('0');
        }else if(n[i] == '9'){
            n[i] = '0', d = 1;
        }else{
            n[i] = n[i] + d, d = 0;
        }
    }
    return findSymmetry(n);
}

string findSmaller(string n){
    int d = -1;
    for(int i = n.size()/2-1; d == -1; i--){
        if(n[i] == '0'){
            n[i] = '9', d = -1;
        }else if(i == 0 && n[i] == '1'){
            n[i] = '9', d = 0; n.pop_back();
        }else{
            n[i] = n[i] + d, d = 0;
        }
    }
    return findSymmetry(n);
}

string nearestPalindromic(string n) {
    if(n.size() == 1) return to_string(n[0]-'1');
    string s[] = {findSymmetry(n), findSmaller(n), findBigger(n)};
    long ns[] = {abs(strtoll(n.c_str(), NULL, 10) - strtoll(s[0].c_str(), NULL, 10)), abs(strtoll(n.c_str(), NULL, 10) - strtoll(s[1].c_str(), NULL, 10)),  abs(strtoll(n.c_str(), NULL, 10) - strtoll(s[2].c_str(), NULL, 10))};
    long i = 0, j = -1, min = LONG_MAX;
    while(i < 3){
        if(ns[i] < min && s[i] != n){
            min = ns[i], j = i;
        }else if(ns[i] == min && s[i] != n){
            if(j == -1){
                min = ns[i], j = i;
            }else if(strtoll(s[i].c_str(), NULL, 10) < strtoll(s[j].c_str(), NULL, 10)){
                min = ns[i], j = i;
            }
        }
        i++;
    }
    return s[j];
}

int main(){
    // string n = "123";
    string n = "11911";
    cout << nearestPalindromic(n) << endl;
    return 0;
}
